---
title: "panel analysis"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}
# setup
library(stargazer)
library(interactions)
library(ggplot2)
library(lm.beta)
df <- read.csv("data_with_media_measures.csv")
head(df)
df_new <- df[!is.na(df$pre_far_left)&!is.na(df$pre_far_right)&!is.na(df$far_left)&!is.na(df$far_right),] 
head(df_new)
write.csv(df_new, "clean_data0928.csv")
df$vf_trumpharmdemo <- (-1)*(df$vf_trumpharmdemo)
df$attitudes_trump <- (df$vf_trumpharmdemo + df$vf_trumpappropriate + df$approve_trump + df$favor_trump)/4

df$trump <- (df$approve_trump + df$favor_trump)/2
df$pre_trump <- (df$pre_approve_trump + df$pre_favor_trump)/2
df$gender <- factor(df$gender, levels = c("Male", "Female"))
df$race <- factor(df$race, levels = c("White", "Black", "Hispanic", "Other"))
```

```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}
# residualize
df$partisan_news_flow_res <- residuals(lm(partisan_news_flow ~ pre_partisan_news_flow, data = df, na.action = na.exclude))
df$outlet_diversity_res <- residuals(lm(outlet_diversity ~ pre_outlet_diversity, data = df, na.action = na.exclude))
df$far_right_res <- residuals(lm(far_right ~ pre_far_right, data = df, na.action = na.exclude))
df$far_left_res <- residuals(lm(far_left ~ pre_far_left, data = df, na.action = na.exclude))

df$trump_res <- residuals(lm(trump ~ pre_trump, data = df, na.action = na.exclude))
df$votecounted_res <- residuals(lm(votecounted ~ pre_votecounted, data = df, na.action = na.exclude))


# standardize
df$partisan_news_flow_res <- (df$partisan_news_flow_res - mean(df$partisan_news_flow_res, na.rm=TRUE))/sd(df$partisan_news_flow_res, na.rm=TRUE)
df$outlet_diversity_res <- (df$outlet_diversity_res - mean(df$outlet_diversity_res, na.rm=TRUE))/sd(df$outlet_diversity_res, na.rm=TRUE)
df$far_right_res <- (df$far_right_res - mean(df$far_right_res, na.rm=TRUE))/sd(df$far_right_res, na.rm=TRUE)
df$far_left_res <- (df$far_left_res - mean(df$far_left_res, na.rm=TRUE))/sd(df$far_left_res, na.rm=TRUE)

df$trump_res <- (df$trump_res - mean(df$trump_res, na.rm=TRUE))/sd(df$trump_res, na.rm=TRUE)
df$votecounted_res <- (df$votecounted_res - mean(df$votecounted_res, na.rm=TRUE))/sd(df$votecounted_res, na.rm=TRUE)

```

# Table 7. Panel models reflecting the effects of change of news use patterns on change of judgment about the election and Trump
```{r echo=FALSE, warning = FALSE, message = FALSE, results = "asis"}

m1 <- lm(trump_res ~ partisan_news_flow_res + outlet_diversity_res + far_right_res + far_left_res + age + gender + race, data = df)

m2 <- lm(votecounted_res ~ partisan_news_flow_res + outlet_diversity_res + far_right_res + far_left_res + age + gender + race, data = df)

m3 <- lm(votecounted_res ~ far_right_res*outlet_diversity_res + partisan_news_flow_res + far_left_res + age + gender + race, data = df)


stargazer(m1, m2, m3,
          single.row = FALSE,
          type = "html",
          digits = 3,
          no.space = TRUE,
          ci = TRUE,
          star.char = c("*", "**", "***"),
          star.cutoffs = c(0.05, 0.01, 0.001),
          notes = c("* p<0.05; ** p<0.01; *** p<0.001"), 
          notes.append = F)


```


